package com.buyouchina.web.action.product;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;

import com.buyouchina.bean.product.Category;
import com.buyouchina.bean.product.Product;
import com.buyouchina.service.product.CategoryService;
import com.buyouchina.service.product.ProductService;
import com.buyouchina.utils.Pagination;
import com.buyouchina.utils.SiteUrl;
import com.buyouchina.web.action.BaseAction;
import com.opensymphony.xwork2.ModelDriven;
import com.opensymphony.xwork2.Preparable;

@Controller("CategoryAction")
public class CategoryAction extends BaseAction implements ModelDriven<Category>,Preparable {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	private Category category;
	@Resource(name = "categoryServiceBean")
	private CategoryService categoryService;
	@Resource(name = "productServiceBean")
	private ProductService productService;
	private List<Category> categoryList = new ArrayList<Category>();
	private Integer categoryid;

	//	查询全部分类
	public String list() {
		Long recordCount = categoryService.getCount();
		Pagination pg = new Pagination(request, response);
		pg.setRecordCount(recordCount.intValue());
		categoryList = categoryService.getScrollData(pg.getFirstResult(),
				pg.getPageSize()).getResultlist();
		request.setAttribute("pagination", pg);
		return SUCCESS;
	}

	//	添加分类
	public String add() {
		categoryService.save(category);
		request.setAttribute("message", "添加分类成功");
		request.setAttribute("urladdress", SiteUrl.readUrl("category.list"));
		return "message";
	}

	//	删除分类
	public String del() {
		
		category=categoryService.find(category.getId());
		
		if(category.getProducts().size()>0){	//	该分类下有产品
			List<Product> products=new ArrayList<Product>(category.getProducts());
			for(int i=0;i<products.size();i++){
				Product product=products.get(i);
				product.setCategory(null);
				productService.update(product);
			}
		}
		categoryService.delete(category.getId());
		request.setAttribute("message", "删除分类成功!");
		request.setAttribute("urladdress", SiteUrl.readUrl("category.list"));
		return MESSAGE;
	}

	//	修改分类
	public String update() {
		String name=category.getName();
		String remark=category.getRemark();
		category=categoryService.find(category.getId());
		category.setName(name);
		category.setRemark(remark);
		categoryService.update(category);
		request.setAttribute("message", "修改分类成功!");
		request.setAttribute("urladdress", SiteUrl.readUrl("category.list"));
		return MESSAGE;
	}

	public Category getModel() {
		return category;
	}
	
	public void prepare() throws Exception {
		if (categoryid == null || "".equals(categoryid)) {
			category = new Category();
		} else {
			category = categoryService.find(categoryid);
		}
	}

	public List<Category> getCategoryList() {
		return categoryList;
	}

	public void setCategoryList(List<Category> categoryList) {
		this.categoryList = categoryList;
	}

	public Category getCategory() {
		return category;
	}

	public void setCategory(Category category) {
		this.category = category;
	}

	public Integer getCategoryid() {
		return categoryid;
	}

	public void setCategoryid(Integer categoryid) {
		this.categoryid = categoryid;
	}

	

}
